#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class RepairRecordProvider : CY.GFive.Core.Providers.DALProvider.IRepairRecordProvider
	{
		#region Sql
		private static readonly string SqlSelectRepairRecord = "SELECT [Id], [ApplicantName],[RoomNbr],[BuildingNbr],[ApplyingDate],[Content],[RepairState],[ApproverName],[Remark] FROM [RepairRecord] WHERE  [Id]=@Id";

		private static readonly string SqlInsertRepairRecord = "INSERT INTO [RepairRecord]([ApplicantName],[RoomNbr],[BuildingNbr],[ApplyingDate],[Content],[RepairState],[ApproverName],[Remark]) values(@ApplicantName,@RoomNbr,@BuildingNbr,@ApplyingDate,@Content,@RepairState,@ApproverName,@Remark);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateRepairRecord = "UPDATE [RepairRecord] SET [ApplicantName] = @ApplicantName,[RoomNbr] = @RoomNbr,[BuildingNbr] = @BuildingNbr,[ApplyingDate] = @ApplyingDate,[Content] = @Content,[RepairState] = @RepairState,[ApproverName] = @ApproverName,[Remark] = @Remark WHERE  [Id]=@Id";

		private static readonly string SqlDeleteRepairRecord = "DELETE [RepairRecord] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllRepairRecord = "SELECT [Id], [ApplicantName],[RoomNbr],[BuildingNbr],[ApplyingDate],[Content],[RepairState],[ApproverName],[Remark] FROM [RepairRecord]";
		#endregion

		#region IRepairRecordProvider Members
		public Core.Business.RepairRecord Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectRepairRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.RepairRecord repairRecord = new Core.Business.RepairRecord();

				if (!reader.IsDBNull(0)) repairRecord.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) repairRecord.ApplicantName = reader.GetString(1);
				if (!reader.IsDBNull(2)) repairRecord.RoomNbr = reader.GetString(2);
				if (!reader.IsDBNull(3)) repairRecord.BuildingNbr = reader.GetString(3);
				if (!reader.IsDBNull(4)) repairRecord.ApplyingDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) repairRecord.Content = reader.GetString(5);
				if (!reader.IsDBNull(6)) repairRecord.RepairState = reader.GetByte(6);
				if (!reader.IsDBNull(7)) repairRecord.ApproverName = reader.GetString(7);
				if (!reader.IsDBNull(8)) repairRecord.Remark = reader.GetString(8);

				return repairRecord;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.RepairRecord repairRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ApplicantName", SqlDbType.NVarChar, repairRecord.ApplicantName);
			sql.AddParameter("@RoomNbr", SqlDbType.NVarChar, repairRecord.RoomNbr);
			sql.AddParameter("@BuildingNbr", SqlDbType.NVarChar, repairRecord.BuildingNbr);
			sql.AddParameter("@ApplyingDate", SqlDbType.DateTime, repairRecord.ApplyingDate);
			sql.AddParameter("@Content", SqlDbType.NVarChar, repairRecord.Content);
			sql.AddParameter("@RepairState", SqlDbType.TinyInt, repairRecord.RepairState);
			sql.AddParameter("@ApproverName", SqlDbType.NVarChar, repairRecord.ApproverName);
			sql.AddParameter("@Remark", SqlDbType.NVarChar, repairRecord.Remark);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertRepairRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) repairRecord.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.RepairRecord repairRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ApplicantName", SqlDbType.NVarChar, repairRecord.ApplicantName);
			sql.AddParameter("@RoomNbr", SqlDbType.NVarChar, repairRecord.RoomNbr);
			sql.AddParameter("@BuildingNbr", SqlDbType.NVarChar, repairRecord.BuildingNbr);
			sql.AddParameter("@ApplyingDate", SqlDbType.DateTime, repairRecord.ApplyingDate);
			sql.AddParameter("@Content", SqlDbType.NVarChar, repairRecord.Content);
			sql.AddParameter("@RepairState", SqlDbType.TinyInt, repairRecord.RepairState);
			sql.AddParameter("@ApproverName", SqlDbType.NVarChar, repairRecord.ApproverName);
			sql.AddParameter("@Remark", SqlDbType.NVarChar, repairRecord.Remark);
			sql.AddParameter("@Id", SqlDbType.Int, repairRecord.Id);
			sql.ExecuteSql(SqlUpdateRepairRecord);
		}

		public void Delete(Core.Business.RepairRecord repairRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, repairRecord.Id);
			sql.ExecuteSql(SqlDeleteRepairRecord);
		}

		public IList<Core.Business.RepairRecord> GetAllRepairRecord()
		{
			IList<Core.Business.RepairRecord> repairRecordlist = new List<Core.Business.RepairRecord>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllRepairRecord);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.RepairRecord repairRecord = new Core.Business.RepairRecord();

					if (!reader.IsDBNull(0)) repairRecord.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) repairRecord.ApplicantName = reader.GetString(1);
					if (!reader.IsDBNull(2)) repairRecord.RoomNbr = reader.GetString(2);
					if (!reader.IsDBNull(3)) repairRecord.BuildingNbr = reader.GetString(3);
					if (!reader.IsDBNull(4)) repairRecord.ApplyingDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) repairRecord.Content = reader.GetString(5);
					if (!reader.IsDBNull(6)) repairRecord.RepairState = reader.GetByte(6);
					if (!reader.IsDBNull(7)) repairRecord.ApproverName = reader.GetString(7);
					if (!reader.IsDBNull(8)) repairRecord.Remark = reader.GetString(8);

					repairRecord.MarkOld();
					repairRecordlist.Add(repairRecord);
				}
				reader.Close();
			}
			return repairRecordlist;
		}
		#endregion
	}
}
